<?php
/**

*/
class SignupController extends App_Controller_Action{

	public function preDispatch()
	{
		$user = $this->getUserId();
		if (!empty($user)) {
			// If the user is logged in, can't really signup again without logging out
			$this->_redirect('/client');
		}
	}

	public function indexAction()
	{
		$request = $this->getRequest();

		$form = new Form_Signup();
		$this->view->form = $form;
		$this->view->signup_success=false;

		if ($this->getRequest()->isPost()) {

			//check IP
			$ip=Model_ActionLog::getIp();
			if (1==0){
				$this->view->error_msg = 'IP address not allowed.';				
			}else{
			
				$formData = $this->getRequest()->getPost();
	
				if ($form->isValid($formData)) {
	
					$email = $formData['email'];
					$password = $formData['password'];
					//timezone = $formData['timezone'];
	
					$model_client = new Model_UserClient();
					$client_id = $model_client->signupClient($email, $password);
					
					if ($client_id!=-1){
						
		    			$body = "Dear YesUp User,<br><br>
									Thank you for signing up an account with us!<br><br>
									Please activate your account by clicking on the following link:<br><br>
									<a href='https://account.yesup.com/signup/activate/id/".$client_id."' target=_blank>http://account.yesup.com/signup/activate/id/".$client_id."</a><br><br>
									
									YesUp Account Username: ".$email." <br><br>
									
									Please feel free to contact us if you have any questions.<br><br>
									
									Best regards,<br><br>
									
									YesUp Account Support Team<br>
										    			";
					    $mail = new Zend_Mail('UTF-8');
					    $mail->setBodyHtml($body)
					         ->setFrom('no-reply@yesup.com', 'YesUp Account')
					         ->addTo($email, '')
					         ->setSubject("Please activate your YesUp Account")
					         ->send();    			
	    			
	    			
						//$this->_redirect("/default/login/client/msg/signup");	
						$this->_redirect("/signup/confirm");	
						
					}else{
						$this->_redirect("/login/client/msg/"."Sorry, email already exists in system or data error.");					
					}
					
				} else {
	
					$form->populate($formData);
				}
			}
		}
		else{
/*
			$timezone = App_Timezone::getDefaultTimeZone();
			$form->populate(array('timezone'=>$timezone));
*/
		}

	}

	public function confirmAction()
	{
	}


	public function activateAction()
	{
		$request = $this->getRequest();
    	$client_id = $request->getParam('id'); 	
    	if (!empty($client_id)){    		
			$model_client = new Model_UserClient();
			$result = $model_client->activateClient($client_id);
			if ($result)    		
				$this->_redirect("/login/client/msg/signup");
			else	    		
				$this->view->error_msg="Sorry, account activation failed.";					
    	}
		
	}
	

	public function resendAction()
	{
		$request = $this->getRequest();

		$email =  $request->getParam('email');
	
		$model_client = new Model_UserClient();
		$client_info = $model_client->getClientByEmail($email);
			
		if (!empty($client_info)){
			$status=$client_info['status'];
			$client_id=$client_info['client_id'];
			if ($status=='Pending')
			{
	    		$body = "Dear YesUp User,<br><br>
							Thank you for signing up an account with us!<br><br>
							Please activate your account by clicking on the following link:<br><br>
							<a href='https://account.yesup.com/signup/activate/id/".$client_id."' target=_blank>http://account.yesup.com/signup/activate/id/".$client_id."</a><br><br>
							
							YesUp Account Username: ".$email." <br><br>
							
							Please feel free to contact us if you have any questions.<br><br>
							
							Best regards,<br><br>
							
							YesUp Account Support Team<br>
								    			";
			    $mail = new Zend_Mail('UTF-8');
			    $mail->setBodyHtml($body)
			         ->setFrom('no-reply@yesup.com', 'YesUp Account')
			         ->addTo($email, '')
			         ->setSubject("Please activate your YesUp Account")
			         ->send();    			
	    	
	    	
				//$this->_redirect("/default/login/client/msg/signup");	
				$this->_redirect("/signup/confirm");	
			
			}else{
				echo 'Account is suspended. Please contact the admin about this.';
				exit;
				
			}
		}
		else{
			echo 'Account not exist. Please <a href="/signup">signup an Yesup account with your email address.';
			exit;
		}						
	}
	
}


